-- ChristmasHouse
-- Created by chengb Nov/4/2016
-- 圣诞小屋

-- 初始化
function initChristmasHouse(self)
    self.item:removeAllChildren();
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            if not DungeonM.isAllMonsterDead() then
                -- 还有怪物存活
                alert(buildingInfo["fail_tip"]);
                return;
            end

            local elemDbase = DungeonM.queryElement(self.gridData.element, "dbase");
            if type(elemDbase["require_equip"]) == "number" and
               not EquipM.isEquiped(elemDbase["require_equip"]) then
                -- 未穿戴指定装备
                self:openBuildingView(nil, nil, buildingInfo["dialog2"], buildingInfo["view_text2"]);
                return;
            end

            local function okFunc()
                -- 进入夹层
                local unrealId = elemDbase["unreal_id"];
                DungeonActionM.go("start_unreal", self.gridData:getPos(), unrealId);
            end

            self:openBuildingView2(okFunc);
        end
    end
    self.icon:addTouchEventListener(onClicked);
end
